package com.sky.query;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@ApiModel(description = "分页查询条件")
@NoArgsConstructor
public class PageQuery {
    @ApiModelProperty("页码")
    private Integer page = 1;
    @ApiModelProperty("每页条数")
    private Integer pageSize = 5;
    @ApiModelProperty("排序字段")
    private String sortBy;
    @ApiModelProperty("是否升序")
    private Boolean isASC = true;

    public <T> Page<T> toMpPage(OrderItem ... items){
        // 1. 分页条件
        Page<T> page = Page.of(this.page,pageSize);
        // 2. 排序条件
        if (StrUtil.isNotBlank(sortBy)){
            page.addOrder(new OrderItem(sortBy,isASC));
        }else {
            page.addOrder(items);
        }

        return page;
    }

    public <T> Page<T> sortBy(String defaultSortBy, Boolean defaultIsAsc){
        return toMpPage(new OrderItem(defaultSortBy,defaultIsAsc));
    }

    public <T> Page<T> sortByCrateTime(){
        return toMpPage(new OrderItem("create_time",false));
    }
    public <T> Page<T> sortByUpdateTime(){
        return toMpPage(new OrderItem("update_time",false));
    }
}
